package desktop.playerguide.aboutmodel;

import desktop.playerguide.PlayerGuideScene;
import pulpcore.Stage;
import pulpcore.animation.Easing;
import pulpcore.animation.Timeline;
import pulpcore.image.Colors;
import pulpcore.image.CoreFont;
import pulpcore.image.CoreImage;
import pulpcore.image.filter.DropShadow;
import pulpcore.image.filter.Filter;
import pulpcore.image.filter.FilterChain;
import pulpcore.image.filter.Stroke;
import pulpcore.scene.Scene2D;
import pulpcore.sprite.Button;
import pulpcore.sprite.FilledSprite;
import pulpcore.sprite.Label;
import pulpcore.util.StringUtil;
import static pulpcore.image.Colors.WHITE;
import static pulpcore.image.Colors.gray;

public class About extends Scene2D {
	
	public String[] messageText = 
    { "Thanks ", "to ", "play ", "the ", "JColumns ", "\u201cJe\u0306rry Mou\u0302se\u201d" };
	 
	 public String backgroundText ="The software is called \"JColumns\", which could be explained as \"the "+
	 "Columns game developed by JerryMouse team\", or simply \"Columns written " +
	 "in java\". Somebody even says that it sounds like \"Jiong Columns\". Anyway,"+
    "it\'s a network game similar to the Tetris."+
    "Beyond the implementation of traditional single game, it achieves the goal "+
    "of play at the local area network, wide area network and web pages based on "+
    "the OpenSocial platform."+
    "The purpose of our project is to implement an open game platform concludes "+
    "several aspects such as interesting model, friendly view, easy control "+
    "and above all, interactive to players.You can visit our main site on http://" +
    "jcolumns.byethost33.com/. And you will find all the infomation you want there."; 
	 
	Button backButton;
	
	public void load(){
		add(new FilledSprite(WHITE));
        
		//add back button
		CoreImage[] buttonArray = new CoreImage[3];
		for (int i = 0; i < buttonArray.length; i++) {
			buttonArray[i] = CoreImage.load("backbutton" + i + ".jpg");
		}
		backButton = new Button(buttonArray,Stage.getWidth()*0.5,Stage.getHeight()*0.75);
        add(backButton);
		// Add word-wrapped background text
        int x = 50;
        int y = 45;
        int startTime = 0;
        int maxWidth = Stage.getWidth() - x*2;
        CoreFont bgFont = CoreFont.load("serif.font.png").tint(gray(128));
        String[] textLines = StringUtil.wordWrap(this.backgroundText, bgFont, maxWidth);
        for (String line : textLines) {
            // Add the sprite
            Label label = new Label(bgFont, line, x, y);
            add(label);
            y += label.height.get() + 5;
            
            // Animate (typing effect)
            int numChars = line.length();
            label.numDisplayChars.set(0);
            label.numDisplayChars.animateTo(numChars, 30*numChars, Easing.NONE, startTime);
            startTime += 30*numChars + 100;
        }
        
        DropShadow dropShadow = new DropShadow(0, 3);
        dropShadow.radius.set(2);
        Stroke stroke = new Stroke(Colors.WHITE, 2);
        Filter filter = new FilterChain(stroke, dropShadow);
    
        // Add messages (play in a loop)
        Timeline timeline = new Timeline();
        startTime = 1000;
        CoreFont messageFont = CoreFont.load("complex.font.png");
        for (String line : this.messageText) {
            // Add the sprite
            int labelWidth = -1; // auto
            int labelHeight = 10;
            Label label = new Label(messageFont, line, 2*Stage.getWidth() / (line.length()), 
            			Stage.getHeight() / 2, labelWidth, labelHeight);
            label.alpha.set(0);
            label.setFilter(filter);
            add(label);
            
            // Animate (zoom)
            timeline.at(startTime).animate(label.alpha, 0, 255, 500);
            timeline.at(startTime).animate(label.height, 10, 102, 1500, Easing.STRONG_OUT);
            timeline.at(startTime).animate(label.angle, -0.1, 0.1, 2000);
            timeline.at(startTime + 1750).animate(label.alpha, 255, 0, 250);
            startTime += 2000;
        }
        
        timeline.loopForever();
        addTimeline(timeline);
	}
	
	@Override
	public void update(int elapsedTime) {
		super.update(elapsedTime);
		if (backButton.isClicked()) {
			Stage.popScene();
			Stage.pushScene(new PlayerGuideScene());
		}
	}
}
